package com.raulraja.processor.impl;

import com.raulraja.processor.ProcessingEngine;
import com.raulraja.processor.Processor;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * Default impl of the processing engine based on a spring configured service.
 * Delegates processing calls to the right processor
 */
public class DefaultProcessingEngineImpl<Target> implements ProcessingEngine<Target> {

	private final static Logger log = Logger.getLogger(DefaultProcessingEngineImpl.class);

	/**
	 * A map of processor that maps object types to a processor that know how to process it
	 */
	private Map<Class<Target>, Processor<Target>> processors;

	public void setProcessors(Map<Class<Target>, Processor<Target>> processors) {
		this.processors = processors;
	}

	/**
	 * Processes something
	 *
	 * @param target the target object to be processed
	 */
	public void process(Target target) {
		log.debug("started processing " + target);
		Processor<Target> processor = processors.get(target.getClass());
		if (processor != null) {
			processor.process(target);
		} else {
			log.debug("processor not found for " + target);		
		}
		log.debug("finished processing " + target);
	}
}
